package bean;

import bean.orderbook.GOrderBookImpl;
import bean.orderbook.IOrderBook;
import com.google.common.collect.Lists;

import core.EngineApi;
import core.EngineCore;
import handler.BaseHandler;
import handler.match.StockMatchHandler;
import handler.pub.L1PubHandler;
import handler.risk.ExistRiskHandler;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.Producer;
import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ShortObjectHashMap;
import org.eclipse.collections.impl.set.mutable.primitive.LongHashSet;
import producer.ExchangeProducer;
import thirdpart.hq.MatchData;

import java.util.*;

@Log4j2
@ToString
@Getter
@RequiredArgsConstructor
public class EngineConfig {

    private short id;

    private String orderRecvIp;
    private int orderRecvPort;

    private String seqUrlList;

    private String pubIp;
    private int pubPort;

    @NonNull
    private String fileName;


    public void startup() throws Exception {
        //1.读取配置文件
//        initConfig();

        //2.数据库连接
//        initDB();

        //3.启动撮合核心
        startEngine();

        //4.建立总线连接 初始化数据的发送
//        initPub();

        //5.初始化接收排队机数据以及连接
//        startSeqConn();


    }

    ////////////////////////////////////////////////////////////////////
    // 初始化消息发布总线

    ////////////////////////////////////////////////////////////////////

    private void startEngine() throws Exception {

        Producer producer =  ExchangeProducer.getProducer();
        //1.前置风控处理器 初始给一个空集合

        final BaseHandler riskHandler = new ExistRiskHandler(new LongHashSet());


        //2.撮合处理器(订单簿*****) 撮合/提供行情查询
        IntObjectHashMap<IOrderBook> orderBookMap = new IntObjectHashMap<>();
        List<Integer> stockList = new ArrayList<>();
        // 为每个stock生成一个ordermap
        stockList.add(123456);
        stockList.forEach(code -> orderBookMap.put(code, new GOrderBookImpl(code)));
        final BaseHandler matchHandler = new StockMatchHandler(orderBookMap);

        //3.发布处理器
        ShortObjectHashMap<List<MatchData>> matcherEventMap = new ShortObjectHashMap<>();
        List<Short> memberList = new ArrayList<>();
        memberList.add((short) 123456);
        memberList.add((short) 987654);
        memberList.forEach(
                id ->matcherEventMap.put(id,Lists.newArrayList()));
        final BaseHandler pubHandler = new L1PubHandler(matcherEventMap);


        engineApi = new EngineCore(
                riskHandler,
                matchHandler,
                pubHandler,
                producer).getApi();

    }

    ////////////////////////////////数据库查询///////////////////////////////////////////


    @Getter
    private EngineApi engineApi;



    /**
     * 初始化
     *
     * @throws Exception
     */
    private void initConfig() throws Exception {
        Properties properties = new Properties();
        properties.load(this.getClass().getResourceAsStream("/" + fileName));

        id = Short.parseShort(properties.getProperty("id"));

        orderRecvIp = properties.getProperty("orderrecvip");
        orderRecvPort = Integer.parseInt(properties.getProperty("orderrecvport"));

        seqUrlList = properties.getProperty("sequrllist");

        pubIp = properties.getProperty("pubip");
        pubPort = Integer.parseInt(properties.getProperty("pubport"));


    }


}
